import fs from 'fs';
import path from 'path';
const addBook = (req, res) => {
    if (!req.body.bookname) {
        res.status(400);
        res.send('提交的数据有误，请检查');
        return;
    }
    fs.readFile(path.join(__dirname, './data/book.json'), (err, data) => {
        if (err) {
            res.status(500);
            res.send('服务器内部错误');
            return;
        }
        const books = JSON.parse(data.toString());
        const book = Object.assign(Object.assign({ id: books[books.length - 1].id + 1 }, req.body), { createdAt: new Date(), updateAt: new Date() });
        books.push(book);
        fs.writeFile(path.join(__dirname, './data/book.json'), JSON.stringify(books), err => {
            if (err) {
                res.status(500);
                res.send('500');
                return;
            }
            const result = {
                message: '添加成功',
                data: book
            };
            res.send(JSON.stringify(result));
        });
    });
};
const delBook = (req, res) => {
    console.log("delBook");
};
export { addBook, delBook };
